package cvte.d20210830.t1;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

/**
 * <li style="color: red;">Prob</li>
 *
 * <li style="color: red;">Desc</li>
 *
 * <li style="color: red;">Link</li>
 *
 * @author habitplus
 * @since 2021-08-30 19:44
 */
public class Main {

    public int[] indicators(int[] arr) {
        if (arr == null || arr.length < 1) return null;
        Stack<List<Integer>> stack = new Stack<>();
        int n = arr.length;
        int[] ans = new int[n];

        for (int i = 0; i < n; i++) {
            while (!stack.isEmpty() && arr[stack.peek().get(0)] < arr[i]) {
                List<Integer> list = stack.pop();

                for (int j : list) ans[j] = i - j;
            }

            if (!stack.isEmpty() && arr[stack.peek().get(0)] == arr[i]) {
                stack.peek().add(i);
            } else {
                List<Integer> tmp = new ArrayList<>();
                tmp.add(i);
                stack.push(tmp);
            }
        }
        return ans;
    }
}
